package br.ufpb.sistcomp.persistence;

import java.util.ArrayList;
import java.util.List;
import br.ufpb.sistcomp.database.InformationDatabase;
import br.ufpb.sistcomp.entity.Semester;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Class that handles the persistence of Semester.
 * 
 * @author Diego Sousa - www.diegosousa.com
 * @version 1.0 Copyright (C) 2012 SistComp Empresa Jr.
 */

public class SemesterPersistence {

	private static SemesterPersistence instance;
	private static SQLiteDatabase sqliteDatabase;
	private ContentValues values;

	private SemesterPersistence() {
		this.values = new ContentValues();
	}

	public static synchronized SemesterPersistence getInstance(Context context) {

		if (instance == null) {
			instance = new SemesterPersistence();

			sqliteDatabase = context.openOrCreateDatabase(
					InformationDatabase.NAME_DATABASE, Context.MODE_PRIVATE,
					null);
		}
		return instance;
	}

	public void insertSemester(Semester semester) {

		values.put(InformationDatabase.ATRIBUTE_SEMESTERS_NAME,
				semester.getName());
		sqliteDatabase.insert(InformationDatabase.GETNAME_TABLE_SEMESTERS,
				null, values);
	}

	public void removeSemester(String nameSemester) {
		sqliteDatabase.delete(InformationDatabase.GETNAME_TABLE_SEMESTERS,
				InformationDatabase.ATRIBUTE_SEMESTERS_NAME + "="
						+ nameSemester, null);
	}

	public void updateSemester(Semester semester) {

		String where = InformationDatabase.ATRIBUTE_SEMESTERS_ID_SEMESTERS
				+ "=" + semester.getId();

		values.put(InformationDatabase.ATRIBUTE_SEMESTERS_ID_SEMESTERS,
				semester.getId());
		values.put(InformationDatabase.ATRIBUTE_SEMESTERS_NAME,
				semester.getName());

		sqliteDatabase.update(InformationDatabase.GETNAME_TABLE_SEMESTERS,
				values, where, null);
	}

	public Semester locateSemester(String nameSemester) {

		Semester semester = null;
		Cursor cursorSemester = sqliteDatabase.query(
				InformationDatabase.GETNAME_TABLE_SEMESTERS, new String[] {
						InformationDatabase.ATRIBUTE_SEMESTERS_ID_SEMESTERS,
						InformationDatabase.ATRIBUTE_SEMESTERS_NAME }, null,
				null, null, null, null);
		
		// course is before the last line, the cursor moves to next line.
		if (cursorSemester.getPosition() == -1) {
			cursorSemester.moveToFirst();
		}
		if (cursorSemester != null) {
			while (!cursorSemester.isAfterLast()) {// Repeat until the last
													// line.
				if (nameSemester
						.equals(cursorSemester
								.getString(
										cursorSemester
												.getColumnIndex(InformationDatabase.ATRIBUTE_SEMESTERS_NAME))
								.toString())) {
					semester = new Semester();
					semester.setId(cursorSemester.getInt(cursorSemester
							.getColumnIndex(InformationDatabase.ATRIBUTE_SEMESTERS_ID_SEMESTERS)));
					semester.setName(cursorSemester.getString(cursorSemester
							.getColumnIndex(InformationDatabase.ATRIBUTE_SEMESTERS_NAME)));
					return semester;
				} else {
					cursorSemester.moveToNext();
				}
			}// end while.
		}
		return null;
	}

	public List<Semester> listAllSemesters() {
		Semester semester = null;
		ArrayList<Semester> listSemester = new ArrayList<Semester>();

		Cursor cursorSemester = sqliteDatabase.query(
				InformationDatabase.GETNAME_TABLE_SEMESTERS, new String[] {
						InformationDatabase.ATRIBUTE_SEMESTERS_ID_SEMESTERS,
						InformationDatabase.ATRIBUTE_SEMESTERS_NAME }, null,
				null, null, null, null);

		// course is before the last line, the cursor moves to next line.
		if (cursorSemester.getPosition() == -1) {
			cursorSemester.moveToFirst();
		}

		if (cursorSemester != null) {
			while (!cursorSemester.isAfterLast()) {// repeats until the last.

				semester = new Semester();
				semester.setId(cursorSemester.getInt(cursorSemester
						.getColumnIndex(InformationDatabase.ATRIBUTE_SEMESTERS_ID_SEMESTERS)));
				semester.setName(cursorSemester.getString(cursorSemester
						.getColumnIndex(InformationDatabase.ATRIBUTE_SEMESTERS_NAME)));
				listSemester.add(semester);
				cursorSemester.moveToNext();
			}
		}
		return listSemester;
	}
}
